/**
 * Created by yangrourou on 16/11/14.
 */
class commonController{
    //构造方法
    constructor(){
        commonController.prototype.token = "";
        commonController.prototype.responseData = "";
    }
    doPost(options){
        console.log(JSON.stringify(options));
        var _self = this;
        $.ajaxSettings.async = false;
        $.ajax({
            type: "post",
            url: "/zt.php?s=/home/index/doPost",
            headers :options.headers,
            dataType : 'json',
            data: options.data,
            success: function(data,state,xhr){
                var jsonData = JSON.parse(data);
                console.log(data);
                //Token无效或过期
                if (jsonData.code == 400 && jsonData.status == 100) {
                    window.location = '/app/index/view/loginView.php';
                }else if(jsonData.code == 200){
                    //检测token是否更新
                    if (xhr.getResponseHeader('token') != null && xhr.getResponseHeader('token') != '') {
                        _self.setToken(xhr.getResponseHeader('token'));
                        console.log("aaa|"+xhr.getResponseHeader('token'));
                    }
                    _self.responseData = jsonData.data;

                }else{
                    alert(jsonData.msg);
                    window.location = '/app/index/view/loginView.php';
                }

            },
            error : function(data) {
                alert("失败"+JSON.stringify(data));
            }
        });
    }
    get(){
        alert(123);
    }
    getJson(jsonUrl){
        var jsonData;
        $.ajaxSettings.async = false;
        $.getJSON(jsonUrl, function(json){
            jsonData = json;
        });
        return jsonData;
    }
    getConfig(configName){
        return this.getJson("/config/"+configName+".json");
    }
    setToken(token){
        this.token = token;
        localStorage.token = this.token;
    }
    getNewToken(){
        var baseConfig = this.getConfig("config");
        var options = {
            headers:{},
            body:{}
        }
        options.headers.appId = baseConfig.serviceConfig.appID;
        options.headers.appKey = baseConfig.serviceConfig.appKey;
        options.headers.url = baseConfig.serviceConfig.url + "/Token";
        var token = this.doPost(options);
        this.setToken(token);
    }
    getToken(){
        return localStorage.token;
    }
    setCache(cacheName,cacheValue){
        if(cacheValue == null || cacheValue == ''){
            localStorage.removeItem(cacheName);
        }else{
            localStorage[cacheName] = cacheValue;
        }
    }
    getCache(cacheName){
        return localStorage[cacheName];
    }
    rmCache(cacheName){
        return localStorage.removeItem(cacheName);
    }
    clearCache(){
        return localStorage.clear();
    }
    getText(key) {
        //获取当前语言
        var lang = this.getCache('lang');
        if (lang == null) {
            lang = "zh";
        }
        var langConfig = this.getJson('/config/lang/'+lang+'.json');
        return langConfig[key];
    }
}